<template>
  <div class="campusInformation">
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin: 0 0 20px 0"
    >
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯管理</el-breadcrumb-item>
      <el-breadcrumb-item>
        {{ isAdd ? "新增校园资讯" : "修改校园资讯" }}
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="card_box">
      <div class="main">
        <div class="col_box">
          <div class="col_title"></div>
          <el-button type="primary" @click="clickSave">保存资讯</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </div>
        <div class="col_box">
          <div class="col_title">图片：</div>
          <el-upload
            class="upload-demo"
            drag
            :show-file-list="false"
            action="#"
            :http-request="imgUpload"
            :on-success="handleAvatarSuccess"
          >
            <div v-if="initData.imagesUrl == ''">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">点击上传</div>
            </div>
            <div v-if="initData.imagesUrl != ''">
              <img :src="initData.imagesUrl" alt="" width="100%" />
            </div>
          </el-upload>
        </div>
        <div class="col_box">
          <div class="col_title">标题：</div>
          <el-input
            v-model="initData.title"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">内容：</div>
          <el-input
            type="textarea"
            :rows="8"
            placeholder="请输入内容"
            v-model="initData.content"
          >
          </el-input>
        </div>
        <div class="col_box">
          <div class="col_title">内容1：</div>
          <el-input
            type="textarea"
            :rows="8"
            placeholder="请输入内容"
            v-model="initData.content1"
          >
          </el-input>
        </div>
        <div class="col_box">
          <div class="col_title">内容2：</div>
          <el-input
            type="textarea"
            :rows="8"
            placeholder="请输入内容"
            v-model="initData.content2"
          >
          </el-input>
        </div>
        <div class="col_box">
          <div class="col_title">内容3：</div>
          <el-input
            type="textarea"
            :rows="8"
            placeholder="请输入内容"
            v-model="initData.content3"
          >
          </el-input>
        </div>
        <div class="col_box">
          <div class="col_title">内容4：</div>
          <el-input
            type="textarea"
            :rows="8"
            placeholder="请输入内容"
            v-model="initData.content4"
          >
          </el-input>
        </div>
        <div class="col_box">
          <div class="col_title"></div>
          <el-button type="primary" @click="clickSave">保存资讯</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import {
  getByIdNews,
  uploadImageNews,
  getAddNews,
  getNewsEdit,
} from "@/util/news.js";

export default {
  data() {
    return {
      isAdd: false,

      initData: {
        id: null,
        title: "",
        images: "",
        imagesUrl: "",
        content: "",
        publishTime: "",
        content1: "",
        content2: "",
        content3: "",
        content4: "",
        showIn: 0,
        createdAt: "",
      },
    };
  },
  created() {
    // 根据Id判断是新增还是修改
    this.isAdd = this.$route.params.id == 0 ? true : false;
    // 修改 根基id获取资讯数据
    this.getByIdNews();
  },
  mounted() {},
  methods: {
    // 根据id获取修改数据
    async getByIdNews() {
      // 判断是新增还是修改 修改发请求;
      if (this.isAdd) {
        return;
      }
      let { data: res } = await getByIdNews({
        id: this.$route.params.id,
      });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data;
    },
    // 上传图片
    async imgUpload(e) {
      var fm = new FormData();
      fm.append("coverFile", e.file);
      let { data: res } = await uploadImageNews(fm);
      console.log(res);
      this.initData.images = res.data.imageName;
    },
    // 上传图片 成功的回调
    handleAvatarSuccess(res, file) {
      console.log(file);
      this.initData.imagesUrl = URL.createObjectURL(file.raw);
    },
    // 点击按钮保存
    async clickSave() {
      console.log(this.initData);
      // 判空
      if (this.isAddData()) {
        return;
      }
      // 判断是否新增; 新增this.$route.params.id==0
      if (this.$route.params.id != 0) {
        this.initData.id = this.$route.params.id;
      }
      // 修改新增默认 取消发布
      this.initData.showIn = 0;

      let { data: res } =
        this.$route.params.id == 0
          ? await getAddNews(this.initData)
          : await getNewsEdit(this.initData);
      console.log(res);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.$router.push("/admin/campus_information/list");
    },
    //判空
    isAddData() {
      if (this.initData.images == "" || this.initData.images == null) {
        this.$message.error("图片不能为空");
        return true;
      }
      if (this.initData.title == "" || this.initData.title == null) {
        this.$message.error("标题不能为空");
        return true;
      }
      if (this.initData.content == "" || this.initData.content == null) {
        this.$message.error("内容不能为空");
        return true;
      }
      return false;
    },
  },
};
</script>
<style lang="less" scoped>
.card_box {
  min-height: calc(100vh - 200px);
  .header_box {
    display: flex;
    margin-bottom: 24px;
  }
  .main {
    .col_box {
      display: flex;
      align-items: center;
      margin-bottom: 24px;
      .col_title {
        width: 120px;
        text-align: right;
        margin-right: 8px;
      }
      .el-input,
      .el-textarea {
        width: 560px;
      }

      /deep/ .el-upload-dragger {
        width: 340px;
        height: 170px;
      }
      .upload-demo {
        margin-bottom: 0.8rem;
      }
    }
  }
}
</style>
